package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class SetmealImpl implements SetmealService {
    @Autowired
    SetmealMapper setmealMapper;

    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }

    @Override
    public PageResult pageAll(SetmealPageQueryDTO ssmpDto) {
        PageHelper.startPage(ssmpDto.getPage(), ssmpDto.getPageSize());
        Page<SetmealVO> ppSets = setmealMapper.pageAll(ssmpDto.getName(), ssmpDto.getStatus(), ssmpDto.getCategoryId());

        List<SetmealVO> llss = ppSets.getResult();
        long total = ppSets.getTotal();

        PageResult pageResult = new PageResult(total, llss);
        System.out.printf("-----------------");
        System.out.println(llss);
        return pageResult;
    }

    @Override
    public void statusById(Integer status, Long id) {

        Setmeal setmeal = Setmeal.builder()
                .id(id).
                status(status)
                .build();

        setmealMapper.statusById(setmeal);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateSetmealByVo(SetmealVO sstV) {
        Setmeal sst = new Setmeal();
        BeanUtils.copyProperties(sstV, sst);

        List<SetmealDish> sdish = sstV.getSetmealDishes();
        setmealMapper.SetmealUpdate(sst);

        for (SetmealDish sd : sdish) {
            setmealMapper.deleteDishById(sst.getId());
        }

        for (int i = 0; i < sdish.size(); i++) {
            sdish.get(i).setSetmealId(sst.getId());
            setmealMapper.setmealDishAdd(sdish.get(i));
        }
    }

    @Override
    public SetmealVO setmealById(Long id) {
        SetmealVO kk = setmealMapper.setmealById(id);
        System.out.println("-------------------------------------------");
        System.out.println(kk);
        return kk;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteSetmealByIds(List<Long> ids) {
        for (int i = 0; i < ids.size(); i++) {
            SetmealVO k = setmealMapper.setmealById(ids.get(i));
            if(k.getStatus()==1){
                throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ON_SALE);
            }
        }

        for (int i = 0; i < ids.size(); i++) {
            setmealMapper.deleteDishById(ids.get(i));
        }
        setmealMapper.deleteByIds(ids);

    }

    @Override
    public void setmealAdd(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);

        List<SetmealDish> kk = new ArrayList<>();
        kk = setmealDTO.getSetmealDishes();

        setmealMapper.setmealAdd(setmeal);

        for (int i = 0; i < kk.size(); i++) {
            kk.get(i).setSetmealId(setmeal.getId());
            setmealMapper.setmealDishAdd(kk.get(i));
        }

    }
}
